package com.sinosoft.sm.user.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinosoft.sm.system.common.SystemContants;
import com.sinosoft.sm.system.common.entity.PaginationEntity;
import com.sinosoft.sm.system.common.exception.ObjectIsExistedException;
import com.sinosoft.sm.system.common.exception.ObjectIsNotExistedException;
import com.sinosoft.sm.system.common.utils.StringUtil;
import com.sinosoft.sm.system.common.utils.ajax.ResponseData;
import com.sinosoft.sm.user.entity.UserEntity;
import com.sinosoft.sm.user.service.api.UserService;
import com.sinosoft.sm.user.web.dto.UserData;


/**
 * 用户控制层
 * @author leichao
 */
@Controller
@RequestMapping("user")
public class UserController {

	@Autowired
	UserService userService;
	
	/**
	 * 根据查询条件获取用户信息
	 * 若userData为空，则返回数据为空
	 * @param userData	用户信息查询条件
	 * @return	回执数据
	 */
	@RequestMapping(value="getUsersByParamsService", method=RequestMethod.POST)
	@ResponseBody
	public ResponseData getUsersByParamsService(UserData UserData) {
		if(UserData == null) {
			return new ResponseData(true, null);
		}
		UserEntity entity = UserData.toUserEntity();
		PaginationEntity paginationEntity = null;
		paginationEntity = userService.getUsers(entity, UserData.getPage(), UserData.getRows());
		if(paginationEntity == null) {
			return new ResponseData(true, null);
		} else {
			ResponseData response = new ResponseData(true, paginationEntity.getTotal(), getUserDatas(paginationEntity.getData()));
			return response;
		}
	}
	

	/**
	 * 根据查询条件获取用户信息
	 * 若userData为空，则返回数据为空
	 * @param userData	用户信息查询条件
	 * @return	回执数据
	 */
	@RequestMapping(value="addUserServiceDD", method=RequestMethod.POST)
	@ResponseBody
	public ResponseData addUserService(UserData userData){
		if(userData == null) {
			return new ResponseData(true, null);
		}
		try {
			boolean result = userService.addUser(userData.toUserEntity());
			return new ResponseData(result, null);
		} catch (ObjectIsExistedException e) {
			return new ResponseData(false, null, e.getMessage(), null);
		}
	}
	
	/**
	 * 重置用户密码（默认密码：123456）
	 * 若捕获异常ObjectIsNotExistedException，则返回异常消息
	 * @param username	用户帐号
	 * @return	回执数据
	 */
	@RequestMapping(value="resetUserPasswordService", method=RequestMethod.POST)
	@ResponseBody
	public ResponseData resetUserPassword(String username) {
		try {
			boolean result = userService.updateUserPassword(username,SystemContants.USER_DEFAULT_PASSWORD);
			return new ResponseData(result, null);
		} catch (ObjectIsNotExistedException e) {
			return new ResponseData(false, null, e.getErrorMsg(), null);
		}
	}
	
	/**
	 * 启用禁用用户。state:1启用  0禁用
	 * @param username
	 * @param state
	 * @return
	 */
	@RequestMapping(value="enableOrDisableUserService", method=RequestMethod.POST)
	@ResponseBody
	public ResponseData enableOrDisableUserServicce(String username,String state){
		if(StringUtil.isBlank(state)||StringUtil.isBlank(username)) {
			return new ResponseData(true, null);
		}
		try {
			boolean result = userService.updateUserState(username,state);
			return new ResponseData(result, null);
		} catch (ObjectIsNotExistedException e) {
			return new ResponseData(false, null, e.getErrorMsg(), null);
		}
	}
	
	/**
	 * 用户密码校验
	 * 若捕获异常ObjectIsNotExistedException，则返回异常消息
	 * @param username	用户帐号
	 * @param password	密码
	 * @return	回执数据
	 */
	@RequestMapping(value="checkUserPasswordService", method=RequestMethod.POST)
	@ResponseBody
	public ResponseData checkUserPassword( String username, String password) {
		try {
			boolean result = userService.checkUserPassword(username, password);
			return new ResponseData(result, null);
		} catch (ObjectIsNotExistedException e) {
			return new ResponseData(false, null, e.getMessage(), null);
		}
	}
	
	/**
	 * 更新用户密码
	 * 若捕获异常ObjectIsNotExistedException，则返回异常消息
	 * @param aid	应用编号
	 * @param username	用户帐号
	 * @param oldPassword	旧密码
	 * @param newPassword	新密码
	 * @return	回执数据
	 */
	@RequestMapping(value="updateUserPasswordService", method=RequestMethod.POST)
	@ResponseBody
	public ResponseData updateUserPassword(String username, String oldPassword, String newPassword) {
		try {
			boolean result = userService.checkUserPassword(username, oldPassword);
			if(result == false) {
				return new ResponseData(false, null, "用户密码不匹配", null);
			} else {
				return new ResponseData(userService.updateUserPassword(username, newPassword), null);
			}
		} catch (ObjectIsNotExistedException e) {
			return new ResponseData(false, null, e.getErrorMsg(), null);
		}
	}
	
	/**
	 * 分页查询获取用户传输数据
	 * 将实体数据集合转换为传输数据集合
	 * @param users	用户实体数据集合
	 * @return	用户传输数据集合
	 */
	@SuppressWarnings("unchecked")
	private List<UserData> getUserDatas(Object datas) {
		List<UserEntity> users = (List<UserEntity>) datas;
		int size = users.size();
		List<UserData> userDatas = new ArrayList<UserData> ();
		for(int i=0; i<size; i++) {	//	循环将实体数据转换为传输数据
			UserData userData = new UserData();
			userData.toUserData(users.get(i));
			userDatas.add(userData);
		}
		return userDatas;
	}
	
}
